package com.wr.recruit.service.service.impl;

import com.wr.recruit.service.apis.dto.CompanyDto;
import com.wr.recruit.service.dao.dbo.CompanyDbo;
import com.wr.recruit.service.dao.repo.CompanyRepo;
import com.wr.recruit.service.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;

import java.util.Objects;

import static com.wr.recruit.service.utils.Constants.COMPANY_NAME;

@Service
public class CompanyServiceImpl implements CompanyService {

    private CompanyRepo companyRepo;

    @Autowired
    public CompanyServiceImpl(CompanyRepo companyRepo) {
        this.companyRepo = companyRepo;
    }

    @Override
    @Transactional
    public CompanyDbo createCompanyInfo(CompanyDbo company) {
        CompanyDbo allByCompanyName = companyRepo.findAllByCompanyName(company.getCompanyName());
        if (Objects.isNull(allByCompanyName))
            return companyRepo.save(company);
        return null;
    }

    @Override
    @Transactional
    public CompanyDbo updateCompanyInfo(CompanyDbo company) {
        CompanyDbo allById = companyRepo.findAllById(company.getId());
        if(!Objects.isNull(allById))
            return companyRepo.save(company);
        return null;
    }

    @Override
    @Transactional
    public Page<CompanyDbo> getAllCompanyList(int pageSize, int pageNum) {
        Specification<CompanyDbo> specification = new Specification<CompanyDbo>() {
            @Override
            public Predicate toPredicate(Root<CompanyDbo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.equal(root.get("isRevoked").as(Boolean.class),false);
                query.where(p1);
                return query.getRestriction();
            }
        };
        return companyRepo.findAll(specification, new PageRequest(pageNum - 1, pageSize));

    }

    @Override
    @Transactional
    public Page<CompanyDbo> getCompanyListByCompanyName(String companyName, int pageSize, int pageNum) {
        Specification<CompanyDbo> specification = new Specification<CompanyDbo>() {
            @Override
            public Predicate toPredicate(Root<CompanyDbo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.equal(root.get("isRevoked").as(Boolean.class),false);
                Predicate p2 = cb.like(root.get("companyName").as(String.class), "%"+companyName+"%");
                query.where(cb.and(p1,p2));
                return query.getRestriction();
            }
        };
        return companyRepo.findAll(specification,new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    @Transactional
    public void RevokeCompanyInfo(String companyCode) {
        CompanyDbo companyDbo = companyRepo.findAllByCompanyCode(companyCode);
        //公司虚拟删除，数据库仍有备份
        if(!Objects.isNull(companyDbo))
            companyDbo.setRevoked(true);
    }

    @Override
    public CompanyDbo getCompanyInfoByCompanyCode(String companyCode) {
        return companyRepo.findAllByCompanyCode(companyCode);
    }
}
